package com.kcs.fxmargin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.action.form.SetupCustomerInformationActionForm;
import com.kcs.fxmargin.bo.CustomerInformationBO;
import com.kcs.fxmargin.model.MCustInfo;
import com.kcs.fxmargin.service.CustInfoService;

public class SetupCustomerInformationAction extends FXAction {
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
	CustInfoService service = new CustInfoService();
	
	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{ 
		try{

			log.debug("SetupCustomerInformationAction : Start");
			
			SetupCustomerInformationActionForm formBean = (SetupCustomerInformationActionForm) form;
			request.setAttribute("customerStatusList", formBean.getCustomerStatusList());
			
			if("init".equals(formBean.getAction())){
				formBean.resetExpect();
				formBean.setCustomerInformationBOList(null);
				formBean.setCustomerStatusList(service.getCustomerStatus());
			}
			else if("search".equals(formBean.getAction())) search(formBean, request);
			else if("delete".equals(formBean.getAction())) delete(formBean, request);
			
			
			
			log.debug("SetupCustomerInformationAction : End");	
		}catch(Exception e){
			log.error(e.getMessage());
			request.setAttribute(Constant.ERR_ATTR, e.getMessage().replaceAll("\"","'"));
		}
		
		
		return mapping.findForward("setupCustomerInformation");
	}
	
	public void search(SetupCustomerInformationActionForm formBean, HttpServletRequest request) throws Exception {
		log.debug("SetupCustomerInformationAction : search : Start");
		formBean.setAction("init");
		
		MCustInfo custInfo = new MCustInfo();
		custInfo.setCifNo(formBean.getCifNo());
		custInfo.setCustName(formBean.getCustomerName());
		
		formBean.setCustomerInformationBOList(service.searchCustomerInfo(custInfo));
		request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
		
		log.debug("SetupCustomerInformationAction : search : End");
	}
	
	public void delete(SetupCustomerInformationActionForm formBean, HttpServletRequest request) throws Exception {
		log.debug("SetupCustomerInformationAction : delete : Start");
		formBean.setAction("init");
		
		if(formBean.getDeleteId() != null && !"".equals(formBean.getDeleteId())){
			String result = service.delete(formBean.getDeleteId());
			
			if("s".equals(result)){
				CustomerInformationBO custBo = null;
				
				for(CustomerInformationBO bo : formBean.getCustomerInformationBOList()){
					if(formBean.getDeleteId().equals(bo.getId())){
						custBo = bo;
					}
				}
				
				formBean.getCustomerInformationBOList().remove(custBo);
				
				int index = 1;
				
				for(CustomerInformationBO bo : formBean.getCustomerInformationBOList()){
					bo.setRowNum(index);
					index++; 
				}
			}
			
		}
		request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
				
		log.debug("SetupCustomerInformationAction : delete : End");
	}
}
